#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "stdio.h"
#include "stdlib.h"

#include "crypto.h"

//密钥
const unsigned char key[]="abcdefgh";
unsigned char key_len=strlen((const char*)key);

//明文
const unsigned char plaintext[]="12345678";
   
int main()
{
	int ret=0;
	int  out_len=8;
	unsigned char *des_out, *base64_out, *out;
	
	
	printf("\r\nkey|%d: %s\r\n\r\n", key_len, key);
	
	printf("====================plaintext des encrypt===================\r\n");
	out_len = strlen((const char*)plaintext); 
	printf("plaintext|%d: %s\r\n", out_len, plaintext);
	//des 加密
	ret=des_encrypt(DES_ECB, key, key_len, NULL, plaintext, &des_out, &out_len);
	printf("des_encrypt res=%d, len=%d, out=", ret, out_len);
	printHex(des_out, out_len);
	//base64 编码
	ret=base64_en(des_out, &base64_out, &out_len); 
	printf("base64_en res=%d, len=%d, out=%s\r\n", ret, out_len, base64_out);
	free(base64_out); 
	free(des_out); 
	printf("=======================================\r\n\r\n");

	printf("====================ciphertext des decrypt===================\r\n");
	out_len = strlen((const char*)base64_out); 
	printf("ciphertext|%d: %s\r\n", out_len, base64_out);
	//base64 解码
	ret=base64_de(base64_out, &out, &out_len); 
	printf("base64_de res=%d, len=%d, out=", ret, out_len);
	printHex(out, out_len);
	//des 解密
	ret=des_decrypt(DES_ECB, key, key_len, NULL, out, &des_out, &out_len);
	des_out[out_len]='\0';
	printf("des_decrypt res=%d, len=%d, out=%s\r\n", ret, out_len, des_out);
	free(des_out); 
	free(out); 	
	printf("=======================================\r\n\r\n");
}
